package com.example.usercenter.controller;
import com.example.usercenter.common.BaseResponse;
import com.example.usercenter.common.ErrorCode;
import com.example.usercenter.common.ResultUtils;
import com.example.usercenter.exception.BussinessException;
import com.example.usercenter.model.domain.User;
import com.example.usercenter.model.domain.request.UserRegisterRequest;
import com.example.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")

public class UserController {

    @Resource

    private  UserService userService;

    @PostMapping("/register")

    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest){

        if(userRegisterRequest==null){
            throw  new BussinessException(ErrorCode.NULL_ERROR);
        }

        String userAccount=userRegisterRequest.getUserAccount();
        String userPassword=userRegisterRequest.getUserPassword();
        String checkPassword=userRegisterRequest.getCheckPassword();

        if(StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){

            return  ResultUtils.error(ErrorCode.PARAMS_ERROR);

        }
        long result=userService.userRegister( userAccount, userPassword, checkPassword);

        return ResultUtils.success(result);
    }


    @PostMapping("/login")

    public User UserLoginRequest(@RequestBody UserRegisterRequest userRegisterRequest, HttpServletRequest request){

        if(userRegisterRequest==null){
            return  null;
        }

        String userAccount=userRegisterRequest.getUserAccount();
        String userPassword=userRegisterRequest.getUserPassword();

        if(StringUtils.isAnyBlank(userAccount,userPassword)){
            return  null;
        }
        return  userService.userLogin( userAccount, userPassword,request);
    }

    @PostMapping("/layout")

    public int userLayout(HttpServletRequest request){

        return  userService.userLayout(request);
    }

}
